<?php

namespace app;

use epii\server\Args;
use service\admin\PrebookSettingService;
use service\admin\PrebookTimeService;

class prebook_setting extends base
{
    public function index(){
        $hospitalId = $this->adminInfo['hospital_id'];
        if(Args::isPost()){

            $num = Args::params('num/d/1');
            $day = Args::params('day/d/1');
            $hour = Args::params('hour/d/1');

            $starts = Args::params('start');
            $ends = Args::params('end');

            if($num<=0){
                $this->error();
            }

            if($day<=0){
                $this->error();
            }
            if($hour<0){
                $this->error();
            }

            if(!$starts || !$ends){
                $this->error();
            }

            $dataList = [];
            foreach($starts as $k=>$v){
                $dataList[] = [
                    'start'=>$v,
                    'end'=>$ends[$k],
                    'hospital_id'=>$hospitalId
                ];
            }
            PrebookSettingService::addOrEdit($hospitalId,$num,$day,$hour);
            PrebookTimeService::addAll($hospitalId,$dataList);
            $this->success();
        }else{

            $setting = PrebookSettingService::getInfoByHospitalId($hospitalId);

            if(!$setting){
                $setting = ['num'=>1,'day'=>7];
            }
            $this->assign('setting',$setting);
            $list = PrebookTimeService::getList(['hospital_id'=>$hospitalId]);
            $this->assign('list',$list);
            $this->adminUiDisplay();
        }
    }
}